package org.example.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.entity.CalculateRequest;
import org.example.service.JythonExecutorService;
import org.example.service.PythonExecutorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/python")
public class PythonController {

    @Autowired
    private PythonExecutorService pythonExecutorService;

    @Autowired
    private JythonExecutorService jythonExecutorService;

    @PostMapping("/calculate")
    public ResponseEntity<Map<String, Object>> calculate(@RequestBody CalculateRequest request) {
        try {
            String result = pythonExecutorService.executePythonScript(
                    "calculate.py",
                    String.valueOf(request.getA()),
                    String.valueOf(request.getB()),
                    request.getOperation()
            );

            // 解析JSON结果
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> resultMap = mapper.readValue(result, Map.class);
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("status", "error");
            errorResponse.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    @GetMapping("/calculate1")
    public ResponseEntity<Map<String, Object>> calculate(@RequestParam String pythonCode) {
        try {
            jythonExecutorService.loadPythonScript(pythonCode);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("status", "ok");
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    @PostMapping("/calculate-async")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> calculateAsync(
            @RequestBody CalculateRequest request) {

        return pythonExecutorService.executePythonScriptAsync(
                "calculate.py",
                String.valueOf(request.getA()),
                String.valueOf(request.getB()),
                request.getOperation()
        ).thenApply(result -> {
            try {
                ObjectMapper mapper = new ObjectMapper();
                Map<String, Object> resultMap = mapper.readValue(result, Map.class);
                return ResponseEntity.ok(resultMap);
            } catch (Exception e) {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("status", "error");
                errorResponse.put("message", e.getMessage());
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(errorResponse);
            }
        });
    }
}
